#!/usr/bin/python -S
"""
stream_test.py: Tests for stream.py
"""

__author__ = 'Andy Chu'


import sys
import unittest

import ioloop
import stream  # module under test


class StreamTest(unittest.TestCase):
  def setUp(self):
    pass

  def tearDown(self):
    pass

  def testStdin(self):
    io_loop = ioloop.instance()
    s = stream.Stdin()

    def echo(chunk):
      print '>>>', repr(chunk)
      print '###', len(chunk)
    s.on('data', echo)

    def end():
      print 'ending'
      ioloop.instance().stop()
    s.on('close', end)

    print 'starting'
    io_loop.start()

  def testLoop(self):
    """2 files at the same time."""

    io_loop = ioloop.instance()
    s = stream.Stdin()
    def echo(chunk):
      print '>>>', repr(chunk)
      print '###', len(chunk)
    s.on('data', echo)

    # NOTE: Failing because you can't listen on a disk file!  Have to turn it
    # into a pipe.
    #f = open('ioloop.py')
    #s2 = stream.FileStream(f)
    #def echo2(chunk):
    #  print '$$$', repr(chunk)
    #  print '???', len(chunk)
    #s2.on('data', echo2)
    #s2.on('end', ioloop.instance().stop())

    io_loop.start()


if __name__ == '__main__':
  unittest.main()
